package com.asa.tongji;

import java.util.Iterator;

import com.asa.hutils.MathHUtils;

/**
 * 计算协方差矩阵，通用算法
 * @author asa
 *
 */
public class Covariance {

	
	/**
	 * 协方差矩阵
	 * @param suanzi	要计算的数据
	 * @return	协方差矩阵
	 */
	public static double[][] f(double[][] suanzi){
		double[][] d = MathHUtils.getD(suanzi, 1);
		d = MathHUtils.zhuanzhi(d);
		d = MathHUtils.chenfa(d, suanzi);
		d = MathHUtils.chenfa(d,1.0/suanzi.length);

//		MathHUtils.print(d);
		
		double a[][] = MathHUtils.copy(suanzi);
		
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				a[i][j] = a[i][j] - d[0][j];
			}
		}
		
		double at[][] = MathHUtils.zhuanzhi(a);
		
		double[][] p = MathHUtils.chenfa(at, a);
		
		p = MathHUtils.chenfa(p, 1.0/suanzi.length);
		
//		MathHUtils.print(p);
		
		
		return p;
	}
	
	public static void main(String[] args) {
		
		double[][] a = {{179,74,33},
				{187,80,31},
				{175,71,28}};
		
		double[][] f = f(a);
		MathHUtils.print(f);
	}
	
	
	
	
	
	
	
	
}
